package org.ljy.lotterysystem.service.activitystatus.operater;

import org.ljy.lotterysystem.dao.dataobject.ActivityUserDO;
import org.ljy.lotterysystem.dao.mapper.ActivityUserMapper;
import org.ljy.lotterysystem.service.dto.ConvertActivityStatusDTO;
import org.ljy.lotterysystem.service.enums.ActivityUserStatusEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Component
public class UserOperator extends AbstractActivityOperator{

    @Autowired
    private ActivityUserMapper activityUserMapper;



    @Override
    public Integer sqquance() {
        return 1;
    }

    @Override
    public Boolean needConvert(ConvertActivityStatusDTO statusConvertDTO) {
        Long activityId = statusConvertDTO.getActivityId();
        List<Long> userIds = statusConvertDTO.getUserIds();
        ActivityUserStatusEnum targetStatus = statusConvertDTO.getTargetUserStatus();
        if(activityId == null || userIds == null
                || CollectionUtils.isEmpty(userIds) || targetStatus == null) {
            return false;
        }
        List<ActivityUserDO> userDOList = activityUserMapper.batchSelectByAUIds(activityId,userIds);
        if(userDOList.isEmpty()) {
            return false;
        }
        //查看状态是否一致
        for(ActivityUserDO userDO : userDOList) {
            if(userDO.getStatus().equalsIgnoreCase(statusConvertDTO.getTargetUserStatus().name())) {
                return false;
            }
        }
        return true;
    }

    @Override
    public Boolean convert(ConvertActivityStatusDTO statusConvertDTO) {
        Long activityId = statusConvertDTO.getActivityId();
        List<Long> userIds = statusConvertDTO.getUserIds();
        ActivityUserStatusEnum targetStatus = statusConvertDTO.getTargetUserStatus();
        try {
            activityUserMapper.batchUpdateStatus(activityId,userIds,targetStatus);
            return true;
        }catch (Exception e) {
            return false;
        }
    }
}
